<?php
/**
 * Akimimi front
 * UserPointLogDao
 * by M.Wang
 */
require_once ('../app/core/models/BaseDao.php');

class UserPointLogDao extends BaseDao{

    private $tableName = 'user_point_logs';

    public function __construct(DBUtil $dbUtil){
        parent::__construct( $dbUtil );
        $this->_name = $this->tableName;
    }

    public function getLogsByUserId($int_userid, $int_page=null, $int_limit=null){
        $str_sql = "
            SELECT SQL_CALC_FOUND_ROWS
                *
            FROM `{$this->tableName}`
            WHERE `user_id` = ?
                AND `is_delete` = 0
            ORDER BY `id` DESC
        ";
        if( !is_null( $int_page ) && !is_null( $int_limit ) )
            $str_sql.= ' LIMIT ' . ( $int_page * $int_limit ) . ',' . $int_limit;

        return $this->dbUtil->SelectData( $str_sql, array($int_userid));
    }

    /**
     * 获取当日用户咨询所获得的积分
     *
     * @param int $int_userid
     * @return int
     */
    public function getUserTodayQueriesPoints($int_userid){
        $str_sql = "
            SELECT SUM(`point`) AS `sum` FROM `{$this->tableName}` WHERE `user_id` = ? AND `type` = '2' AND `is_delete` = '0' AND DATE_FORMAT(`created`, '%Y%j') = DATE_FORMAT(NOW(), '%Y%j')
        ";
        $arr_result = $this->dbUtil->SelectData($str_sql, array($int_userid));
        if(empty($arr_result[0]['sum']))
            return 0;
        return $arr_result[0]['sum'];
    }

    /**
     * 获取当日用户发表评论所获得的积分
     *
     * @param int $int_userid
     * @return int
     */
    public function getUserTodayCommentsPoints($int_userid){
        $str_sql = "
            SELECT SUM(`point`) AS `sum` FROM `{$this->tableName}` WHERE `user_id` = ? AND `type` = '3' AND `is_delete` = '0' AND DATE_FORMAT(`created`, '%Y%j') = DATE_FORMAT(NOW(), '%Y%j')
        ";
        $arr_result = $this->dbUtil->SelectData($str_sql, array($int_userid));
        if(empty($arr_result[0]['sum']))
            return 0;
        return $arr_result[0]['sum'];
    }
    
    /**
     * 获取当日用户获得的积分
     *
     * @param int $int_userid
     * @return int
     */
    public function getUserTodayPoints( $int_userid, $int_type ){
        $str_sql = "
            SELECT SUM(`point`) AS `sum` FROM `{$this->tableName}` WHERE `user_id` = ? AND `type` = ? AND `is_delete` = '0' AND DATE_FORMAT(`created`, '%Y%j') = DATE_FORMAT(NOW(), '%Y%j')
        ";
        $arr_result = $this->dbUtil->SelectData( $str_sql, array( $int_userid, $int_type ) );
        if(empty($arr_result[0]['sum']))
            return 0;
        return $arr_result[0]['sum'];
    }

}
?>
